<?php

namespace App\Http\Controllers;

use App\Models\Subject;
use Illuminate\Http\Request;

class SubjectController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $query = Subject::query();
        if ($name = $request->name){
            $query->where('name', 'like', "%{$name}%");
        }
        return response()->json(['data' => $query->paginate(5)],200);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $subject = Subject::create($request->all());
        return response()->json(['data' => $subject], 200);
    }

    /**
     * Display the specified resource.
     */
    public function show(Subject $subject)
    {
       return response()->json(['data' => $subject], 200);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request,Subject $subject)
    {
        $subject->update($request->all());
        return response()->json(['data' => $subject], 200);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Subject $subject)
    {
        $subject->delete();
        return response()->json(['data' => $subject], 200);
    }
}
